package org.jerrymouse.jsa4j.search.kv;

import java.util.HashMap;
import java.util.Map;

import org.jerrymouse.jsa4j.db.kv.DB;
import org.jerrymouse.jsa4j.db.kv.DBManagerFactory;
import org.jerrymouse.jsa4j.db.kv.Indexer;
import org.jerrymouse.jsa4j.db.kv.Repository;
import org.jerrymouse.jsa4j.search.Searcher;
import org.jerrymouse.jsa4j.search.SearcherManager;

public class KVSearcherManager implements SearcherManager {
	private DB db;

	@Override
	public Searcher getSearcher() {
		Repository repository = new Repository(getPrefix(), getDb());
		Indexer indexer = new Indexer(repository);
		KVSearcher kvSearcher = new KVSearcher();
		kvSearcher.setIndexer(indexer);
		return kvSearcher;
	}

	/**
	 * 务必设置DBname 和prefix
	 */
	@Override
	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	private Map<String, String> properties = new HashMap<String, String>();

	private String getDBName() {
		return properties.get("DBName");
	}

	private String getPrefix() {
		return properties.get("prefix");
	}

	public void setDb(DB db) {
		this.db = db;
	}

	private String prefix;

	public DB getDb() {
		if (db == null)
			db = new DBManagerFactory().getDBManager(getDBName()).getDB();
		return db;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
}
